<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">AI会话</div>
      <a-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        class="ms-content"
        @finish="submitForm"
      >
        <div>
          <a-form-item name="email">
            <a-input v-model:value="loginForm.email" placeholder="账号">
              <template #prefix>
                <UserOutlined class="site-form-item-icon" />
              </template>
            </a-input>
          </a-form-item>
          <a-form-item name="pwd">
            <a-input
              v-model:value="loginForm.pwd"
              type="password"
              placeholder="密码"
              disabled
              :maxlength="12"
              show-pwd
            >
              <template #prefix>
                <LockOutlined class="site-form-item-icon" />
              </template>
              <!--              &lt;!&ndash;              <template #suffix>&ndash;&gt;-->
              <!--              &lt;!&ndash;                <div>{{ timeStr }}秒</div>&ndash;&gt;-->
              <!--              &lt;!&ndash;              </template>&ndash;&gt;-->
            </a-input>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" html-type="submit" style="width: 100%">
              登录
            </a-button>
          </a-form-item>
        </div>
      </a-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
// import { usePermissStore } from '@/store/permiss';
import { message } from 'ant-design-vue';
import type { Rule } from 'ant-design-vue/es/form';
import { getUserToken } from '@/api';

interface LoginInfo {
  email: string;
  pwd: string;
}
const loginFormRef = ref();
const router = useRouter();
const loginForm = reactive<LoginInfo>({
  email: '15171493390',
  pwd: '666666',
});

const validateEmail = async (_rule: Rule, value: string) => {
  if (value === '') {
    return Promise.reject('手机号不允许为空');
  } else {
    const reg = /^1[3-9]\d{9}$/.test(value);
    if (!reg) {
      return Promise.reject('请输入正确的手机号');
    }
    return Promise.resolve();
  }
};

const rules: Record<string, Rule[]> = {
  email: [
    {
      required: true,
      validator: validateEmail,
      trigger: ['blur', 'change'],
    },
  ],
  pwd: [
    {
      required: true,
      message: '密码不允许为空',
      trigger: ['blur', 'change'],
    },
  ],
};
const submitForm = async () => {
  try {
    const res = await getUserToken({
      phone: loginForm.email,
      validateCode: '666666',
    });
    message.success('登录成功');
    localStorage.setItem('user_token', res.data.jwt);
    await router.push('/aiTest');
  } catch {
    return false;
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../assets/offic.png);
  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #000;
  font-weight: bold;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
:deep(.a-form-item__error) {
  left: 45px;
}
</style>
